package com.jwds.digital.bios.business.integration.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.jwds.digital.bios.business.core.infrastructure.dto.external.BlendMatchDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.feign.BasicStockBinReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.vo.feign.BasicStockBinRespVO;
import com.jwds.digital.bios.business.core.utils.FeignResultUtil;
import com.jwds.digital.bios.business.integration.service.ExternalDataProcess;
import com.jwds.digital.bios.business.rpc.feign.BasicStockBinFeign;
import com.jwds.tdc.framework.common.exception.BusinessException;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Author hesong
 */
@Service
public class L32BC04 implements ExternalDataProcess {

    @Autowired
    private BasicStockBinFeign stockBinFeign;

    @Override
    public String getMessageId() {
        return this.getClass().getSimpleName();
    }

    @Override
    public void addProcess(Object dataObj) throws BusinessException {
        BlendMatchDTO blendMatchDTO = BeanUtil.toBean(dataObj, BlendMatchDTO.class);
        flushMatchData(blendMatchDTO);
    }

    /**
     * 刷新料仓选中和放仓流量设定
     * @param blendMatchDTO 消息实体
     */
    private void flushMatchData(BlendMatchDTO blendMatchDTO) {
        //查询所有预配料仓
        BasicStockBinReqDTO reqDTO = new BasicStockBinReqDTO();
        reqDTO.setGroupCode("YPLC-T");
        List<BasicStockBinRespVO> slotList = FeignResultUtil.getDataFromBaseResponse(stockBinFeign.list(reqDTO));

        BigDecimal totalFlow = new BigDecimal(blendMatchDTO.getTotalFlow());
        String mixRatio = blendMatchDTO.getMixRatio();
        Map map = BeanUtil.toBean(mixRatio, Map.class);

        List<BasicStockBinReqDTO> updateList = Lists.newArrayList();
        for (BasicStockBinRespVO basicStockBinRespVO : slotList) {
            BasicStockBinReqDTO basicStockBinReqDTO = new BasicStockBinReqDTO();
            basicStockBinReqDTO.setId(basicStockBinRespVO.getId());
            Object value = map.get(basicStockBinRespVO.getSlotCode());
            if (null != value) {
                basicStockBinReqDTO.setOutInUse(true);
                BigDecimal ratio = new BigDecimal(value.toString());
                BigDecimal slotFlow = totalFlow.multiply(ratio).setScale(2);
                basicStockBinReqDTO.setSetSpeed(slotFlow);
            } else {
                basicStockBinReqDTO.setOutInUse(false);
            }
            updateList.add(basicStockBinReqDTO);
        }
        if (CollectionUtil.isNotEmpty(updateList)) {
            stockBinFeign.batchUpdate(updateList);
        }
    }

    @Override
    public void updateProcess(Object dataObj) throws BusinessException {
        BlendMatchDTO blendMatchDTO = BeanUtil.toBean(dataObj, BlendMatchDTO.class);
        flushMatchData(blendMatchDTO);
    }

    @Override
    public void deleteProcess(Object dataObj) throws BusinessException {
        //此接口无法接受删除业务
    }
}
